#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(void)
{
	key_t key;
	int shmid;
	int proj_id;

	key=IPC_PRIVATE;
	shmid=shmget(key,1024,0660);
	if(shmid==-1)
		perror("cannot create a shared memory segment");
	else
		printf("1. key=IPC_PRIVATE, shared memory segment shmid= %d\n",shmid);

	proj_id=1;
	key=ftok("/home/program",proj_id);
	if(key==-1)
		perror("cannot generate IPC key");

	shmid=shmget(key,1024,IPC_CREAT|0660);
        if(shmid==-1)
                perror("cannot create a shared memory segment");
	else
        	printf("2. key=%d generated by ftok, shared memory shmid= %d\n",key,shmid);
	
	shmid=shmget(key,1024,IPC_CREAT |IPC_EXCL |0660);
	if(shmid==-1)
		perror("cannot create a shared memory segment");

	shmid=shmget(key,1024,IPC_CREAT |0660);
	if(shmid==-1)
		perror("cannot create a shared memory segment");	
	else
		printf("Access the existing shared memory segment\n");

	return 0;
}
